package edu.calpoly.android.echoes;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemLongClickListener;

public class SaveListAdapter extends BaseAdapter implements OnItemLongClickListener {

	/**
	 * The application Context in which this JokeListAdapter is being used.
	 */
	private Context m_context;

	/**
	 * The dataset to which this JokeListAdapter is bound.
	 */
	private List<String> m_saveList;

	/**
	 * The position in the dataset of the currently selected Joke.
	 */
	private int m_nSelectedPosition;

	/**
	 * Parameterized constructor that takes in the application Context in which
	 * it is being used and the Collection of Joke objects to which it is bound.
	 * m_nSelectedPosition will be initialized to Adapter.NO_SELECTION.
	 * 
	 * @param context
	 *            The application Context in which this JokeListAdapter is being
	 *            used.
	 * 
	 * @param jokeList
	 *            The Collection of Joke objects to which this JokeListAdapter
	 *            is bound.
	 */
	public SaveListAdapter(Context context, List<String> saveList) {
		m_context = context;
		m_saveList = saveList;
		m_nSelectedPosition = Adapter.NO_SELECTION;
	}

	/**
	 * Accessor method for retrieving the position in the dataset of the
	 * currently selected Joke.
	 * 
	 * @return an integer representing the position in the dataset of the
	 *         currently selected Joke.
	 */
	public int getSelectedPosition() {
		return m_nSelectedPosition;
	}


	public int getCount() {
		return m_saveList.size();
	}


	public Object getItem(int position) {
		return m_saveList.get(position);
	}


	public long getItemId(int position) {
		return position;
	}


	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		Log.d("onItemLongClick", "selected " + arg2);
		m_nSelectedPosition = arg2;
		return false;
	}


	public View getView(int position, View convertView, ViewGroup parent) {
		SaveView saveView = null;
		
		if (convertView == null) {
			saveView = new SaveView(m_context, m_saveList.get(position));
		}
		else {
			saveView = (SaveView)convertView;
			saveView.setSave(m_saveList.get(position));
		}
		
		return saveView;
	}
}

